#include "iptwindow.h"
#include "ui_iptwindow.h"


iptwindow::iptwindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::iptwindow)
{
    ui->setupUi(this);
    //g=new Graph<QString>;
    ui->stackedWidget->setCurrentIndex(0);
    ui->stackedWidget_2->setCurrentIndex(0);
    ui->stackedWidget_3->setCurrentIndex(0);
    openTable();// 打开数据库
    tabmodel=new QSqlTableModel(this,DB);
    theSeletion=new QItemSelectionModel(tabmodel);

}

iptwindow::~iptwindow()
{
    delete ui;
}


void iptwindow::on_currentChanged(const QModelIndex &current, const QModelIndex &previous)
{

}

void iptwindow::on_currentRowChanged(const QModelIndex &current, const QModelIndex &previous)
{

}

void iptwindow::openTable()

{
    DB = QSqlDatabase::addDatabase("QSQLITE"); //添加数据库驱动
    //QString fn=f.fileName();
    //QString fn=QFileDialog::getOpenFileName(this,tr("选择数据文件"));
    DB.setDatabaseName("C:/Users/11930/DataGripProjects/first/identifier.sqlite");       //这里输入你的数据库名
    DB.exec("SET NAMES 'GBK'");
    bool ok=DB.open();
    qDebug()<<ok;
    if (!ok){
        QMessageBox::information(nullptr, "infor", "open failed");
        qDebug()<<"error open database because"<<DB.lastError().text();
       }
}

void iptwindow::openTablecity()
{
    tabmodel=new QSqlTableModel(this,DB);
    tabmodel->setTable("city");//数据表格
    tabmodel->setSort(tabmodel->fieldIndex("prevname"),Qt::AscendingOrder);//以升序排序
    tabmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);// 表格修改策略，手动提交
    if(!tabmodel->select())
    {
        QMessageBox::critical(this,"error","打开数据表错误.错误信息\n"+tabmodel->lastError().text());
        return;
    }
    //else
    //    QMessageBox::information(nullptr, "infor", "success");
    theSeletion=new QItemSelectionModel(tabmodel);
    ui->tableView->setModel(tabmodel);
    tabmodel->setHeaderData(tabmodel->fieldIndex("prevname"),Qt::Horizontal,"城市");
    tabmodel->setHeaderData(tabmodel->fieldIndex("nextname"),Qt::Horizontal,"城市");
    tabmodel->setHeaderData(tabmodel->fieldIndex("length"),Qt::Horizontal,"距离(km)");//将列头设置成中文
    //ui->tableView->setColumnHidden(tabmodel->fieldIndex("sid"),true);//隐藏某列
}

void iptwindow::openTableplane()
{
    tabmodel=new QSqlTableModel(this,DB);
    tabmodel->setTable("plane");//数据表格
    tabmodel->setSort(tabmodel->fieldIndex("sid"),Qt::AscendingOrder);//以升序排序
    tabmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);// 表格修改策略，手动提交
    if(!tabmodel->select())
    {
        QMessageBox::critical(this,"error","打开数据表错误.错误信息\n"+tabmodel->lastError().text());
        return;
    }
    //else
    //    QMessageBox::information(nullptr, "infor", "success");
    theSeletion=new QItemSelectionModel(tabmodel);
    ui->tableView_2->setModel(tabmodel);
    tabmodel->setHeaderData(tabmodel->fieldIndex("prevname"),Qt::Horizontal,"城市");
    tabmodel->setHeaderData(tabmodel->fieldIndex("nextname"),Qt::Horizontal,"城市");
    tabmodel->setHeaderData(tabmodel->fieldIndex("length"),Qt::Horizontal,"距离(km)");
    tabmodel->setHeaderData(tabmodel->fieldIndex("sid"),Qt::Horizontal,"飞机航班");
    tabmodel->setHeaderData(tabmodel->fieldIndex("cost"),Qt::Horizontal,"花费");
    tabmodel->setHeaderData(tabmodel->fieldIndex("time"),Qt::Horizontal,"所需时间");//将列头设置成中文
    //ui->tableView->setColumnHidden(tabmodel->fieldIndex("sid"),true);//隐藏某列
}

void iptwindow::openTabletrain()
{
    tabmodel=new QSqlTableModel(this,DB);
    tabmodel->setTable("train");//数据表格
    tabmodel->setSort(tabmodel->fieldIndex("sid"),Qt::AscendingOrder);//以升序排序
    tabmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);// 表格修改策略，手动提交
    if(!tabmodel->select())
    {
        QMessageBox::critical(this,"error","打开数据表错误.错误信息\n"+tabmodel->lastError().text());
        return;
    }
    //else
    //    QMessageBox::information(nullptr, "infor", "success");
    theSeletion=new QItemSelectionModel(tabmodel);
    ui->tableView_3->setModel(tabmodel);
    tabmodel->setHeaderData(tabmodel->fieldIndex("prevname"),Qt::Horizontal,"城市");
    tabmodel->setHeaderData(tabmodel->fieldIndex("nextname"),Qt::Horizontal,"城市");
    tabmodel->setHeaderData(tabmodel->fieldIndex("length"),Qt::Horizontal,"距离(km)");
    tabmodel->setHeaderData(tabmodel->fieldIndex("sid"),Qt::Horizontal,"列车班次");
    tabmodel->setHeaderData(tabmodel->fieldIndex("cost"),Qt::Horizontal,"花费");
    tabmodel->setHeaderData(tabmodel->fieldIndex("time"),Qt::Horizontal,"所需时间");//将列头设置成中文
    //ui->tableView->setColumnHidden(tabmodel->fieldIndex("sid"),true);//隐藏某列
}

void iptwindow::getFiledNames()
{

}



void iptwindow::ButtonMutex()
{
    ui->radioButton->setChecked(false);
    ui->radioButton_2->setChecked(false);
    ui->pushButton_3->setChecked(false);
    ui->pushButton_6->setChecked(false);
    ui->pushButton_15->setChecked(false);
    ui->pushButton_16->setChecked(false);
    ui->pushButton_17->setChecked(false);
    ui->traincost->setChecked(false);
    ui->traintime->setChecked(false);
    ui->planecost->setChecked(false);
    ui->planetime->setChecked(false);
    ui->stackedWidget_2->setCurrentIndex(0);
    ui->stackedWidget_3->setCurrentIndex(0);
}


void iptwindow::on_pushButton_5_clicked()// 查看航班信息
{
    ButtonMutex();
    ui->stackedWidget->setCurrentIndex(2);
    openTableplane();
}


void iptwindow::on_pushButton_clicked()// 查看城市信息
{
    ButtonMutex();
    ui->stackedWidget->setCurrentIndex(1);
    openTablecity();
}


void iptwindow::on_pushButton_2_clicked()// 查看列车信息
{
    ButtonMutex();
    ui->stackedWidget->setCurrentIndex(3);
    openTabletrain();
}


void iptwindow::on_pushButton_4_clicked()// 退出
{
    ButtonMutex();
    this->close();
    QMessageBox::information(this,tr("成功!"),tr("再见!"));
}


void iptwindow::on_pushButton_3_clicked()// 最佳出行分析
{
    ui->radioButton->setChecked(false);
    ui->radioButton_2->setChecked(false);
    ui->traincost->setChecked(false);
    ui->traintime->setChecked(false);
    ui->planetime->setChecked(false);
    ui->planecost->setChecked(false);
    ui->pushButton_6->setChecked(false);
    ui->stackedWidget_3->setCurrentIndex(1);
    ui->stackedWidget->setCurrentIndex(0);
    ui->stackedWidget_2->setCurrentIndex(0);
}



void iptwindow::on_pushButton_8_clicked()// 最少花费分析
{

}


void iptwindow::on_pushButton_7_clicked()// 最少时间分析
{

}


void iptwindow::on_pushButton_9_clicked()// 添加数据（城市信息）
{
    /*tabmodel->insertRow(tabmodel->rowCount(),QModelIndex());// 插入行
    QModelIndex curIndex=tabmodel->index(tabmodel->rowCount()-1,1);*/
    QSqlQuery sql(DB);
    QString t1 = ui->lineEdit->text();
    QString t2 = ui->lineEdit_2->text();
    QString t3 = ui->lineEdit_3->text();
    QString mm = QString("insert into city values('%0','%1','%2')").arg(t1).arg(t2).arg(t3);
    sql.exec(mm);
    plane.InsertV(t1,t2);
    train.InsertV(t1,t2);
    QMessageBox::information(this,tr("成功!"),tr("添加成功!"));
    /*theSeletion->clearSelection();// 清空选项
    theSeletion->setCurrentIndex(curIndex,QItemSelectionModel::Select);*/
    /*
    tabmodel->setData(tabmodel->index(curIndex.row(),0),2000+tabmodel->rowCount());
    tabmodel->setData(tabmodel->index(curIndex.row(),2),"nan");*/
}


void iptwindow::on_pushButton_6_clicked()// 数据修改
{
    ui->traincost->setChecked(false);
    ui->traintime->setChecked(false);
    ui->planetime->setChecked(false);
    ui->planecost->setChecked(false);
    ui->pushButton_3->setChecked(false);
    ui->stackedWidget_2->setCurrentIndex(0);
    ui->stackedWidget_3->setCurrentIndex(0);
    ui->stackedWidget_2->setCurrentIndex(2);
    ui->stackedWidget->setCurrentIndex(0);
}


void iptwindow::on_pushButton_15_clicked()// 修改城市信息
{
    ui->pushButton_16->setChecked(false);
    ui->pushButton_17->setChecked(false);
    ui->stackedWidget->setCurrentIndex(4);
}


void iptwindow::on_pushButton_16_clicked()// 修改航班信息
{
    ui->pushButton_15->setChecked(false);
    ui->pushButton_17->setChecked(false);
    ui->stackedWidget->setCurrentIndex(5);
}


void iptwindow::on_pushButton_17_clicked()// 修改列车信息
{
    ui->pushButton_15->setChecked(false);
    ui->pushButton_16->setChecked(false);
    ui->stackedWidget->setCurrentIndex(6);
}


void iptwindow::on_pushButton_11_clicked()// 添加数据（航班信息）
{
    QSqlQuery sql(DB);
    QString t1 = ui->lineEdit_4->text();
    QString t2 = ui->lineEdit_5->text();
    QString t3 = ui->lineEdit_6->text();
    QString t4 = ui->lineEdit_7->text();
    QString t5 = ui->lineEdit_8->text();
    QString t6 = ui->lineEdit_9->text();
    QString mm = QString("insert into plane values('%0','%1','%2','%3','%4','%5')").arg(t1).arg(t2).arg(t3).arg(t4).arg(t5).arg(t6);
    sql.exec(mm);

    plane.InsertE(t2,t3,t5.toDouble(),t6.toDouble(),t4.toDouble(),t1);
    QMessageBox::information(this,tr("成功!"),tr("添加成功!"));
}


void iptwindow::on_pushButton_13_clicked()// 添加数据（列车信息）
{
    QSqlQuery sql(DB);
    QString t1 = ui->lineEdit_10->text();
    QString t2 = ui->lineEdit_11->text();
    QString t3 = ui->lineEdit_12->text();
    QString t4 = ui->lineEdit_13->text();
    QString t5 = ui->lineEdit_14->text();
    QString t6 = ui->lineEdit_15->text();
    QString mm = QString("insert into train values('%0','%1','%2','%3','%4','%5')").arg(t1).arg(t2).arg(t3).arg(t4).arg(t5).arg(t6);
    sql.exec(mm);

    train.InsertE(t2,t3,t5.toDouble(),t6.toDouble(),t4.toDouble(),t1);
    QMessageBox::information(this,tr("成功!"),tr("添加成功!"));
}


void iptwindow::on_pushButton_10_clicked()// 删除数据（城市信息）
{
    QSqlQuery sql(DB);
    QString t1 = ui->lineEdit->text();
    QString t2 = ui->lineEdit_2->text();
    QString t3 = ui->lineEdit_3->text();
    QString mm = QString("delete from city where length = '%0'").arg(t3);
    plane.DeleteE(t1,t2);
    train.DeleteE(t1,t2);
    bool ok = sql.exec(mm);
    if(ok)
        QMessageBox::information(this,tr("成功!"),tr("删除成功!"));
    else
        QMessageBox::critical(this,tr("失败!"),tr("数据库中无该项/n请检查后重试!"));
}


void iptwindow::on_pushButton_12_clicked()// 删除数据（航班信息）
{
    QSqlQuery sql(DB);
    QString t1 = ui->lineEdit_4->text();
    QString t2 = ui->lineEdit_5->text();
    QString t3 = ui->lineEdit_6->text();
    /*
    QString t4 = ui->lineEdit_7->text();
    QString t5 = ui->lineEdit_8->text();
    QString t6 = ui->lineEdit_9->text();
    */
    QString mm = QString("delete from plane where sid='%0'").arg(t1);
    plane.DeleteE(t2,t3);
    bool ok = sql.exec(mm);
    if(ok)
        QMessageBox::information(this,tr("成功!"),tr("删除成功!"));
    else
        QMessageBox::critical(this,tr("失败!"),tr("数据中无该项/n请检查后重试!"));
}



void iptwindow::on_pushButton_14_clicked()// 删除数据（列车信息）
{
    QSqlQuery sql(DB);
    QString t1 = ui->lineEdit_10->text();
    QString t2 = ui->lineEdit_11->text();
    QString t3 = ui->lineEdit_12->text();
    /*
    QString t4 = ui->lineEdit_13->text();
    QString t5 = ui->lineEdit_14->text();
    QString t6 = ui->lineEdit_15->text();
    */
    QString mm = QString("delete from train where sid='%0'").arg(t1);
    train.DeleteE(t2,t3);
    bool ok = sql.exec(mm);
    if(ok)
        QMessageBox::information(this,tr("成功!"),tr("删除成功!"));
    else
        QMessageBox::critical(this,tr("失败!"),tr("数据中无该项/n请检查后重试!"));
}


void iptwindow::on_pushButton_18_clicked()// 修改数据按钮（城市信息）
{
    QSqlQuery sql(DB);
    QString t3 = ui->lineEdit_3->text();
    QString mm = QString("select length='%0' from city").arg(t3);
    bool T2=sql.exec(mm);

    if(T2)
    {
        QMessageBox::information(this,tr("成功!") , tr("请修改！"));
        ui->stackedWidget->setCurrentIndex(7);
    }
    else
        QMessageBox::critical(this ,tr("提示") , tr("该数据不存在，无法更改！"));
}


void iptwindow::on_pushButton_19_clicked()// 修改数据按钮（航班信息）
{
    QSqlQuery sql(DB);
    QString t3 = ui->lineEdit_4->text();
    QString mm = QString("select sid='%0' from plane").arg(t3);

    bool T2=sql.exec(mm);

    if(T2)
    {
        QMessageBox::information(this,tr("成功!") , tr("请修改！"));
        ui->stackedWidget->setCurrentIndex(8);
    }
    else
        QMessageBox::critical(this ,tr("提示") , tr("该数据不存在，无法更改！"));
}


void iptwindow::on_pushButton_20_clicked()// 修改数据按钮（列车信息）
{
    QSqlQuery sql(DB);
    QString t3 = ui->lineEdit_10->text();
    QString mm = QString("select sid='%0' from train").arg(t3);

    bool T2=sql.exec(mm);

    if(T2)
    {
        QMessageBox::information(this,tr("成功!") , tr("请修改！"));
        ui->stackedWidget->setCurrentIndex(9);
    }
    else
        QMessageBox::critical(this ,tr("提示") , tr("该数据不存在，无法更改！"));
}



void iptwindow::on_pushButton_21_clicked()// 确认修改按钮（城市信息）
{
    QSqlQuery sql(DB);
    QString v1 = ui->lineEdit->text();
    QString v2 = ui->lineEdit_2->text();
    QString t1 = ui->lineEdit_16->text();
    QString t2 = ui->lineEdit_17->text();
    QString t3 = ui->lineEdit_18->text();
    QString t4 = ui->lineEdit_3->text();
    QString mm1 = QString("select length = '%0' from city").arg(t3);
    QSqlQuery query(DB);
    query.exec(mm1);
    QString mm = QString("UPDATE city set prevname = '%0',nextname = '%1' ,length = '%2 'where length = '%3' ").arg(t1).arg(t2).arg(t3).arg(t4);
    train.DeleteV(v1,v2);
    train.InsertV(t1,t2);
    plane.DeleteV(v1,v2);
    plane.InsertV(t1,t2);
    bool T2=sql.exec(mm);
    if(T2)
        QMessageBox::information(this ,tr("提示") , tr("更改成功!"));
    else
        QMessageBox::critical(this,tr("提示") , tr("修改失败!"));
}


void iptwindow::on_pushButton_22_clicked()// 取消修改按钮（城市信息）
{
    QMessageBox::information(this ,tr("提示") , tr("取消成功!"));
    ui->stackedWidget->setCurrentIndex(4);
}


void iptwindow::on_pushButton_23_clicked()// 确认修改按钮（航班信息）
{
    QSqlQuery sql(DB);
    QString v1 = ui->lineEdit_5->text();
    QString v2 = ui->lineEdit_6->text();
    QString id = ui->lineEdit_4->text();
    QString t1 = ui->lineEdit_19->text();
    QString t2 = ui->lineEdit_20->text();
    QString t3 = ui->lineEdit_21->text();
    QString t4 = ui->lineEdit_22->text();
    QString t5 = ui->lineEdit_23->text();
    QString t6 = ui->lineEdit_24->text();
    QString mm1 = QString("select sid = '%0' from plane").arg(id);
    QSqlQuery query(DB);
    query.exec(mm1);
    QString mm = QString("UPDATE plane set sid = '%0',prevname = '%1' ,nextname = '%2 ',length = '%3',cost = '%4',time = '%5' where sid = '%6' ").arg(t1).arg(t2).arg(t3).arg(t4).arg(t5).arg(t6).arg(id);
    plane.DeleteE(v1,v2);
    plane.InsertE(t2,t3,t5.toDouble(),t6.toDouble(),t4.toDouble(),t1);
    bool T2=sql.exec(mm);
    if(T2)
        QMessageBox::information(this ,tr("提示") , tr("更改成功!"));
    else
        QMessageBox::critical(this,tr("提示") , tr("修改失败!"));
}


void iptwindow::on_pushButton_25_clicked()// 取消修改按钮（航班信息）
{
    QMessageBox::information(this ,tr("提示") , tr("取消成功!"));
    ui->stackedWidget->setCurrentIndex(5);
}


void iptwindow::on_pushButton_24_clicked()// 确认修改按钮（列车信息）
{
    QSqlQuery sql(DB);
    QString v1 = ui->lineEdit_11->text();
    QString v2 = ui->lineEdit_12->text();
    QString id = ui->lineEdit_10->text();
    QString t1 = ui->lineEdit_25->text();
    QString t2 = ui->lineEdit_26->text();
    QString t3 = ui->lineEdit_27->text();
    QString t4 = ui->lineEdit_28->text();
    QString t5 = ui->lineEdit_29->text();
    QString t6 = ui->lineEdit_30->text();
    QString mm1 = QString("select sid = '%0' from train").arg(id);
    QSqlQuery query(DB);
    query.exec(mm1);
    QString mm = QString("UPDATE train set sid = '%0',prevname = '%1' ,nextname = '%2 ',length = '%3',cost = '%4',time = '%5' where sid = '%6' ").arg(t1).arg(t2).arg(t3).arg(t4).arg(t5).arg(t6).arg(id);
    train.DeleteE(v1,v2);
    train.InsertE(t2,t3,t5.toDouble(),t6.toDouble(),t4.toDouble(),t1);
    bool T2=sql.exec(mm);
    if(T2)
        QMessageBox::information(this ,tr("提示") , tr("更改成功!"));
    else
        QMessageBox::critical(this,tr("提示") , tr("修改失败!"));
}


void iptwindow::on_pushButton_26_clicked()// 取消修改按钮（列车信息）
{
    QMessageBox::information(this ,tr("提示") , tr("取消成功!"));
    ui->stackedWidget->setCurrentIndex(6);
}


void iptwindow::on_radioButton_clicked()
{
    qDebug()<<"飞机";
    ui->stackedWidget_2->setCurrentIndex(1);
}


void iptwindow::on_radioButton_2_clicked()
{
    qDebug()<<"火车";
    ui->stackedWidget_2->setCurrentIndex(3);
}


void iptwindow::on_planetime_clicked()//最短时间分析按钮（飞机）
{
    ui->traincost->setChecked(false);
    ui->traintime->setChecked(false);
    ui->planetime->setChecked(true);
    ui->planecost->setChecked(false);
    ui->stackedWidget->setCurrentIndex(10);

}


void iptwindow::on_traincost_clicked()//最少花费分析按钮（火车）
{
    ui->traincost->setChecked(true);
    ui->traintime->setChecked(false);
    ui->planetime->setChecked(false);
    ui->planecost->setChecked(false);
    ui->stackedWidget->setCurrentIndex(13);
}


void iptwindow::on_planecost_clicked()//最少花费分析按钮（飞机）
{
    ui->traincost->setChecked(false);
    ui->traintime->setChecked(false);
    ui->planetime->setChecked(false);
    ui->planecost->setChecked(true);
    ui->stackedWidget->setCurrentIndex(11);
}


void iptwindow::on_traintime_clicked()//最短时间分析按钮（火车）
{
        ui->traincost->setChecked(false);
        ui->traintime->setChecked(true);
        ui->planetime->setChecked(false);
        ui->planecost->setChecked(false);
        ui->stackedWidget->setCurrentIndex(12);
}


void iptwindow::on_pushButton_27_clicked()//最短时间分析确定分析按钮（飞机）
{
    QString city,ssid;
    plane.djst_planetime(ui->lineEdit_31->text(),ui->lineEdit_32->text(),city,ssid);
    //plane.show();

    ui->label_35->setText(city+ssid);
}

void iptwindow::on_pushButton_29_clicked()//最少花费分析确定分析按钮（飞机）
{
    QString city,ssid;
    plane.djst_cost(ui->lineEdit_31->text(),ui->lineEdit_32->text(),city,ssid);
    //plane.show();

    ui->label_36->setText(city+ssid);
}

void iptwindow::on_pushButton_31_clicked()//最短时间分析确定分析按钮（火车）
{
    QString city,ssid;
    train.djst_traintime(ui->lineEdit_31->text(),ui->lineEdit_32->text(),city,ssid);
    //plane.show();

    ui->label_37->setText(city+ssid);
}

void iptwindow::on_pushButton_33_clicked()//最少花费分析确定分析按钮（火车）
{
    QString city,ssid;
    train.djst_cost(ui->lineEdit_31->text(),ui->lineEdit_32->text(),city,ssid);
    //plane.show();

    ui->label_38->setText(city+ssid);
}

